package sgbag1.Vues;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import java.awt.Polygon;
import java.awt.Rectangle;

import java.awt.RenderingHints;
import java.awt.Shape;

import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;

import java.util.ArrayList;

import java.util.Arrays;

import sgbag1.Noyau.Bagage;
import sgbag1.Noyau.Tapis;

public class VueTapis extends VueElement {
    
    private int noPositions;
    private int largeur = 10;
    private int longueur;
    private double angle = 0.0;
    private Color couleurLibre = new Color(238, 213, 183);
    private Rectangle forme;
    private Tapis tapis;
    private Point positionGuichet;
    
    public VueTapis(Tapis t){
        super(new Point(t.getPosition()));
        tapis = t;
        positionGuichet = new Point(t.getGuichet().getPosition());
        
        noPositions = (int)(60 / t.getVitesse());
        
        calculateForme();
    }
    
    public Tapis getObjetNoyau(){
        return tapis;
    }
    
    public void calculateForme(){
        angle = Math.atan((double)(position.y - positionGuichet.y) / (double)(position.x - positionGuichet.x));
        
        longueur = (int)Point.distance(position.x, position.y, positionGuichet.x, positionGuichet.y);
        forme = new Rectangle(positionGuichet.x, positionGuichet.y - largeur/2, longueur, largeur);
        
        CalculerBoiteEnglobante();
    }
    
    @Override
    public void setTauxConversion(double t){
        super.setTauxConversion(t);
        position = new Point(tapis.getPosition());
        positionGuichet = new Point(tapis.getGuichet().getPosition());
        position.x *= tauxConversion;
        position.y *= tauxConversion;
        positionGuichet.x *= tauxConversion;
        positionGuichet.y *= tauxConversion;

        calculateForme();
    }
    
    @Override
    public void setDecalage(Point p){
        super.setDecalage(p);
        position.translate(decalage.x, decalage.y);
        positionGuichet.translate(decalage.x, decalage.y);

        calculateForme();
    }
    
    @Override
    public void Dessiner(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        
        AffineTransform tempTransform = g2.getTransform();
        
        g2.rotate(angle, positionGuichet.x, positionGuichet.y);
        g2.setColor(couleurLibre);
        g2.fill(forme);
        Color temp = g2.getColor();
        g2.setColor(new Color(255, 0, 0));
        g2.draw(forme);
        
        g2.setColor(new Color(0,0,0));
        
        Bagage bagage = this.tapis.getBagage();
        
        if(bagage != null){
            g2.setColor(new Color(0, 255, 0));
            Point positionBagage = bagage.getPosition();
            
            g2.fillRect((int)(positionBagage.x * tauxConversion + decalage.x), (int)(positionBagage.y * tauxConversion + decalage.y - largeur/2 + 1), largeur-1, largeur-1);
        }
        g2.setTransform(tempTransform);
        super.Dessiner(g);
        
        g2.setColor(temp);
    }
    
    @Override
    public void CalculerBoiteEnglobante(){
        AffineTransform at = AffineTransform.getRotateInstance(
                angle, positionGuichet.x, positionGuichet.y);

        boiteEnglobante = new Polygon();

        PathIterator i = forme.getPathIterator(at);
        int c = 0;
        while (c++ < 4) {
            double[] xy = new double[2];
            i.currentSegment(xy);
            ((Polygon)boiteEnglobante).addPoint((int) xy[0], (int) xy[1]);
            
            i.next();
        }
    }
}
